package com.course.business.controller.admin;

import com.course.server.dto.CourseContentFileDto;
import com.course.server.dto.ResponseDto;
import com.course.server.service.CourseContentFileService;
import com.course.server.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 〈一句话功能简述>
 * 〈〉
 *
 * @Author metype
 * @Create 2021/5/25
 * @Since 1.0.0
 */
@RestController
@RequestMapping("/admin/courseContentFile")
public class CourseContentFileController {

    private static final Logger LOG = LoggerFactory.getLogger(CourseContentFileController.class);
    public static final String BUSINESS_NAME = "课程内容文件";

    @Autowired
    private CourseContentFileService courseContentFileService;

    @GetMapping("/list/{courseId}")
    public ResponseDto<List <CourseContentFileDto>> test(@PathVariable String courseId) {
        LOG.info("courseId:{}", courseId);
        List <CourseContentFileDto> fileDtos = courseContentFileService.list(courseId);
        return new ResponseDto <>(fileDtos);
    }

    @RequestMapping("/save")
    public ResponseDto<CourseContentFileDto> save(@RequestBody CourseContentFileDto courseContentFileDto) {
        LOG.info("courseContentFileDto:{}", courseContentFileDto);
        // 保存校验
        ValidatorUtil.require(courseContentFileDto.getId(), "id");
        ValidatorUtil.require(courseContentFileDto.getCourseId(), "课程id");
        ValidatorUtil.length(courseContentFileDto.getUrl(), "地址", 1 , 100);
        ValidatorUtil.length(courseContentFileDto.getName(), "文件名", 1 , 100);

        courseContentFileService.save(courseContentFileDto);
        return new ResponseDto <>(courseContentFileDto);
    }

    @RequestMapping("/del/{id}")
    public ResponseDto del(@PathVariable String id) {
        LOG.info("id:{}", id);
        courseContentFileService.del(id);
        return new ResponseDto<>("200","删除成功！");
    }

}